package kernel.loaders.binary;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author George
 */
public class BinaryScanner {
    
    private String fileName;
    private FileInputStream fileInputStream;
    private BufferedInputStream bufferedInputStream;
    private File file;
    private int currentByte;

    public BinaryScanner() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    public BinaryScanner(String fileName) {
        this.fileName = fileName;
        this.file = new File(fileName);
        try {
            this.fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            this.fileInputStream = null;
        }      
        this.bufferedInputStream = new BufferedInputStream(fileInputStream);
        this.currentByte = -1;
    }
    
    private void setCurrentByte(int currentByte) {
        if (this.currentByte == -1)
            this.currentByte = currentByte;        
    }
    
    private int getCurrentByte() throws IOException {
        if (this.currentByte == -1) {
            return this.bufferedInputStream.read();
        }
        else {
            int result = this.currentByte;
            this.currentByte = -1;
            return result;
        }
    }
    
    public boolean hasMoreBytes() throws IOException {
        int b = this.bufferedInputStream.read();
        if (b != -1) {
            this.setCurrentByte(b);
            return true;
        }
        else {
            this.setCurrentByte(b);
            return false;
        }        
    } 
    
    public int getNextByte() throws IOException {
        return getCurrentByte();        
    }
    
    public void skip(int numberOfBytes) throws IOException {
        for (int i = 0; i < numberOfBytes; i++) {
            this.getNextByte();            
        }
    }

}
